package com.wdg.service.impl;


import com.wdg.dao.IAroundeqDao;
import com.wdg.dao.IIndexDao;
import com.wdg.pojo.*;
import com.wdg.service.IIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    private IIndexDao iIndexDao;

    @Autowired
    private IAroundeqDao iAroundeqDao;



    @Override
    public List<TodayRecommend> getCookBookForIndex() {
        List<TodayRecommend> cookBookForIndex = iIndexDao.getCookBookForIndex();

        for (TodayRecommend cookbook : cookBookForIndex) {
            cookbook.setStaroffs(iAroundeqDao.getStaroffList(cookbook.getCookbook().getCbid()));
        }

        return cookBookForIndex;
    }

    @Override
    public List<CircleImage> getCircleImageForIndex() {
        return iIndexDao.getCircleImageForIndex();
    }

    @Override
    public List<Cookbook> getNewCookBookForIndex() {
        List<Cookbook> cookBookBy = iIndexDao.getNewCookBookForIndex();

        for (Cookbook cookbook : cookBookBy) {
            cookbook.setStaroffs(iAroundeqDao.getStaroffList(cookbook.getCbid()));
        }
        return cookBookBy;
    }

    @Override
    public List<Food> getHotFoodForIndex() {
        return iIndexDao.getHotFoodForIndex();
    }

    @Override
    public Cookbook getCookBookByCbid(String cbid) {
        Cookbook cookBook = iIndexDao.getCookBookByCbid(cbid);

        cookBook.setCbtoolList(getCbtoolByCbid(cbid)); //设置工具

        cookBook.setCbtrimList(getCbtrimByCbid(cbid)); //设置辅料

        cookBook.setCbmainList(getCbmainByCbid(cbid)); //设置主料

        cookBook.setDetailsList(getCbdetailsByCbid(cbid)); //设置详细步骤

        cookBook.setStaroffs(iAroundeqDao.getStaroffList(cbid)); // 设置收藏列表

        return cookBook;
    }

    @Override
    public List<Cbtool> getCbtoolByCbid(String cbid) {
        return iIndexDao.getCbtoolByCbid(cbid);
    }

    @Override
    public List<Cbtrim> getCbtrimByCbid(String cbid) {
        return iIndexDao.getCbtrimByCbid(cbid);
    }

    @Override
    public List<Details> getCbdetailsByCbid(String cbid) {
        return iIndexDao.getCbdetailsByCbid(cbid);
    }


    @Override
    public List<Cbmain> getCbmainByCbid(String cbid) {
        return iIndexDao.getCbmainByCbid(cbid);
    }

    //分类查询

    @Override
    public List<Foodtitle> getAllSort() {
        List<Foodtitle> allSort = iIndexDao.getAllSort();

        for (Foodtitle sort : allSort){
            List<Food> allFood = getAllFood(sort.getFtid());
            sort.setFoods(allFood);
        }

        return allSort;
    }

    @Override
    public List<Food> getAllFood(int ftid) {
        return iIndexDao.getAllFood(ftid);
    }

    @Override
    public List<Sort> getCookBookByFid(int fid,int pageId) {

        Map<String,Integer> sort_page = new HashMap<>();

        sort_page.put("fid",fid);
        sort_page.put("pageId",pageId);


        List<Sort> sorts = iIndexDao.getCookBookByFid(sort_page);

        for (Sort sort : sorts) {
            Cookbook cookbook = sort.getCookbookList();
            cookbook.setStaroffs(iAroundeqDao.getStaroffList(cookbook.getCbid()));
        }
        return sorts;

    }

    @Override
    public void addCookBookView(String cbid) {
        iIndexDao.adaCookBookView(cbid);
    }
}
